const dataPrx = require('./database');
const fs = require("fs");
var path = require('path');
const TassCmd2 = require('./tass_cmd2');

let STORAGE_CERTS_PATH = path.resolve(__dirname, 'public/certs');;

var Controller2 = Controller2 || {};

Controller2.queryCertList = async(req, res) => {
    let data = await dataPrx.knex()
                        .from('t_tass')
                        .select('*');
    return data;
}

Controller2.editCert = async(req, res) => {
    let ret = TassCmd2.gen_csr(req.body.type == 1, Number(req.body.signIndex), req.dn, path.resolve(STORAGE_CERTS_PATH, req.body.signCsr));
    if(ret == 0) {
        await dataPrx.knex()
        .from('t_tass')
        .update({
            dn: req.body.dn,
            signIndex: Number(req.body.signIndex),
            signCsr: path.join(STORAGE_CERTS_PATH, req.body.signCsr),
        })
        .where({
            type: req.body.type
        });
        return path.join(STORAGE_CERTS_PATH, req.body.signCsr);
    } else {
        return "";
    }
}

Controller2.importCerts = async(req, res) => {
    let file1 = req.files.filter((item)=>item.fieldname == 'file1')[0];
    let file2 = req.files.filter((item)=>item.fieldname == 'file2')[0];
    let file3 = req.files.filter((item)=>item.fieldname == 'file3')[0];
    let path_signCert = path.resolve(STORAGE_CERTS_PATH, file1.originalname);
    let path_encCert = path.resolve(STORAGE_CERTS_PATH, file2.originalname);
    let path_p7b = path.resolve(STORAGE_CERTS_PATH, file3.originalname);
    fs.writeFileSync(path_signCert, file1.buffer);
    fs.writeFileSync(path_encCert, file2.buffer);
    fs.writeFileSync(path_p7b, file3.buffer);

    await dataPrx.knex()
        .from('t_tass')
        .update({
            signIndex: Number(req.body.signIndex),
            encIndex: Number(req.body.encIndex),
            signCert: path.join(STORAGE_CERTS_PATH, file1.originalname),
            encCert: path.join(STORAGE_CERTS_PATH, file2.originalname),
            p7b: path.join(STORAGE_CERTS_PATH, file3.originalname),
        })
        .where({
            type: req.body.type
        });

    return TassCmd2.import_files(req.body.type == 1, path_p7b, req.body.signIndex, req.body.encIndex);
}

Controller2.queryVpnConfs = async(req, res) => {
    let data = await dataPrx.knex()
        .from('t_tass_vpn')
        .select('*');
    return data;
}

Controller2.addVpnConf = async(req, res) => {
    let client_conf = await dataPrx.knex()
        .from('t_tass')
        .select('*')
        .where({type:1})
        .first();
    let server_conf = await dataPrx.knex()
        .from('t_tass')
        .select('*')
        .where({type:2})
        .first();
    let ret = await TassCmd2.add_new_conf(req.body.isRoot, req.body.remoteIp, req.body.remotePort, req.body.localIp, 
        req.body.localClientPort, req.body.localServerPort, 
        client_conf.signCert, client_conf.signIndex, 
        client_conf.encCert, client_conf.encIndex,
        server_conf.signCert, server_conf.signIndex, 
        server_conf.encCert, server_conf.encIndex);
    let obj = {
        uuid: ret,
        server_id: req.body.server_id,
        localIp: req.body.localIp,
        localClientPort: req.body.localClientPort,
        localServerPort: req.body.localServerPort,
        remoteIp: req.body.remoteIp,
        remotePort: req.body.remotePort,
        isRoot: req.body.isRoot,
    };
    if(ret != null) {
        await dataPrx.knex()
        .from('t_tass_vpn')
        .insert(obj);

        return obj;
    } else {
        return ret;
    }
}

Controller2.editVpnConf = async(req, res) => {
    let ret = await TassCmd2.edit_conf(req.body.uuid, req.body.remoteIp, req.body.remotePort, req.body.localClientPort);
    if(ret == 0) {
        await dataPrx.knex()
        .from('t_tass_vpn')
        .update({
            server_id: req.body.server_id,
            localIp: req.body.localIp,
            localClientPort: req.body.localClientPort,
            localServerPort: req.body.localServerPort,
            remoteIp: req.body.remoteIp,
            remotePort: req.body.remotePort
        })
        .where({
            uuid: req.body.uuid
        });
    }
    return ret;
}

Controller2.delVpnConf = async(req, res) => {
    let ret = await TassCmd2.del_conf(req.body.uuid);
    if(ret == 0) {
        await dataPrx.knex()
        .from('t_tass_vpn')
        .where({
            uuid: req.body.uuid
        }).del();
    }
    return ret;
}

Controller2.queryVpnStat = async(req, res) => {
    let ret = await TassCmd2.query_vpn_stat();

    return ret;
}

Controller2.openVpn = async(req, res) => {
    let client_conf = await dataPrx.knex()
                            .from('t_tass')
                            .select('*')
                            .where({type:1})
                            .first();
    let server_conf = await dataPrx.knex()
                            .from('t_tass')
                            .select('*')
                            .where({type:2})
                            .first();
    let ret = await TassCmd2.start_vpn(
        client_conf.signCert, client_conf.signIndex, 
        client_conf.encCert, client_conf.encIndex,
        server_conf.signCert, server_conf.signIndex, 
        server_conf.encCert, server_conf.encIndex);
    return ret;
}

Controller2.closeVpn = async(req, res) => {
    let ret = TassCmd2.close_vpn();
    return ret;
}

module.exports = Controller2;